/**
 * 开始答题的倒计时
 */
export const startCountdown = function() {
	let {
		minutes,
		seconds
	} = this
	this.exercTimer = setInterval(() => {
		if (seconds > 0 || minutes > 0) {
			seconds--
			if (seconds < 0) {
				minutes--
				seconds = 59
			}
			if (minutes < 0) {
				clearInterval(this.exercTimer)
				// 倒计时结束处理
			}
		} else {
			clearInterval(this.exercTimer)
		}
		Object.assign(this, {
			minutes,
			seconds
		})
	}, 1000)
}
/**
 * 递归展开数组
 * @param {Object} wgList
 */
export const expTopic = function(wgList, exercList) {
	if (wgList && Array.isArray(wgList)) {
		wgList.forEach(item => {
			let tmpList = item.widgetList.map(p => {
				return {
					...p.options,
					type: p.type,
					select: ''
				}
			})
			exercList.push(...tmpList)
		})
	}
}

/**
 * 处理最终要提交的数据
 */
export const handleSubmit = function() {
	let {
		submitData: sData,
		exercList
	} = this
	console.log(exercList);
	sData.ansed_count = 0 // 已作答
	sData.ansno_count = 0 // 未作答
	sData.correct_count = 0 // 正确
	sData.error_count = 0 // 错误
	sData.current_score = 0 // 得分
	exercList.forEach(exerc => {
		// 是否作答
		if (!!exerc.select) {
			sData.ansed_count += 1
			// 是否正确
			let tcr = null
			if (exerc.type === 'multiple') {
				tcr = exerc?.correct?.join(',')
			} else {
				tcr = exerc.correct
			}
			if (tcr && tcr === exerc.select) {
				sData.correct_count += 1
				sData.current_score += parseInt(exerc.score)
			} else {
				sData.error_count += 1
			}
		} else {
			sData.error_count += 1; // 没有作答也算作错误
			sData.ansno_count += 1; // 未作答数量加一
		}
	})
	sData.exerc_json = JSON.stringify(exercList)
	sData.end_time = new Date().getTime()
	return sData
}